#!/bin/sh /etc/rc.common

START=99
USB_LOCKFILE="/tmp/usb_restart.lock"


usb_lock()
{
	got_lock=0
	local timeout=15
	if [ ! -e "$USB_LOCKFILE" ] ; then
		touch "$USB_LOCKFILE"
		got_lock=1
	fi
	echo "$got_lock"
}

usb_unlock()
{
	rm -rf "$USB_LOCKFILE"
}


start()
{
	gdisplay=$(uci get gargoyle.system.themes 2>/dev/null)
	webroot=$(uci get gargoyle.global.web_root 2>/dev/null)
	webroot=${webroot:-/www}
	theme_root=$(uci get gargoyle.global.theme_root 2>/dev/null)
	theme_root=${theme_root:-themes}
	common_css=$(uci get gargoyle.global.common_css 2>/dev/null)
	common_css=${common_css:-common.css}
	theme_css="theme.css"

	got_lock=$(usb_lock)
	if [ "$got_lock" = "1" ] ; then

		set -- $(find "${webroot}"/"${theme_root}"/ -name "${theme_css}")
		num_themes="$#"

		if [ -z "${gdisplay}" ] && [ "${num_themes}" -gt 1 ] ; then

			menu_name="Themes"
			[ -x /usr/bin/i18n-menu ] && {
				translation=$(i18n-menu gargoyle.display.system_themes)
				[ -n "$translation" ] && menu_name="$translation"
			}
			uci set gargoyle.display.system_themes="$menu_name"
			uci set gargoyle.scripts.system_themes='themes.sh'
			uci set gargoyle.system.themes='311'
			uci commit gargoyle
		fi
		if [ -n "${gdisplay}" ] && [ "${num_themes}" = 1 ] ; then
			uci del gargoyle.system.themes
			uci commit gargoyle
		fi

	
		#if current theme doesn't exist anymore switch back to default
		selected_theme=$(uci get gargoyle.global.theme  2>/dev/null)
		if [ ! -e "${webroot}"/"${theme_root}"/"${selected_theme}"/"${theme_css}"  ] ; then
			if [ -e "${webroot}"/"${theme_root}"/"Gargoyle"/"${theme_css}" ] ; then
				#should always have default theme, but check above to make sure
				uci set gargoyle.global.theme=Gargoyle
				uci commit gargoyle
			fi
		fi
		usb_unlock
	fi

}
